package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.Headship;
import cn.com.blueInfo.bpm.person.enums.HeadshipLeaderFlag;
import cn.com.blueInfo.bpm.person.mapper.HeadshipMapper;
import cn.com.blueInfo.bpm.person.repository.HeadshipRepository;
import cn.com.blueInfo.bpm.person.request.HeadshipDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 职务信息表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class HeadshipRepositoryImpl
        extends ServiceImpl<HeadshipMapper, Headship>
        implements HeadshipRepository, BaseRepository<Headship, HeadshipDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增职务信息
     * @param headship 职务信息参数
     */
    @Override
    public Integer createHeadship(Headship headship) {
        return sqlExecuteResult(save(headship));
    }

    /**
     * 批量新增职务信息
     * @param headshipList 职务信息参数列表
     */
    @Override
    public Integer batchCreateHeadship(List<Headship> headshipList) {
        return sqlExecuteResult(saveBatch(headshipList), headshipList.size());
    }

    /**
     * 删除职务信息
     * @param headshipId 职务信息ID
     */
    @Override
    public Integer deleteHeadship(Long headshipId) {
        Headship headship = new Headship();
        headship.setId(headshipId);
        headship.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(headship));
    }

    /**
     * 批量删除职务信息
     * @param headshipIdList 职务信息ID列表
     */
    @Override
    public Integer batchDeleteHeadship(List<Long> headshipIdList) {
        List<Headship> headshipList = new ArrayList<>();
        for (Long headshipId : headshipIdList) {
            Headship headship = new Headship();
            headship.setId(headshipId);
            headship.setDeleted(BaseDeleted.DELETED);
            headshipList.add(headship);
        }
        return sqlExecuteResult(removeBatchByIds(headshipList), headshipList.size());
    }

    /**
     * 更新部分职务信息
     * @param headship 职务信息参数
     */
    @Override
    public Integer updateHeadshipPartial(Headship headship) {
        validateUpdateId(headship.getId());
        LambdaUpdateWrapper<Headship> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Headship::getId, headship.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部职务信息
     * @param headship 职务信息参数
     */
    @Override
    public Integer updateHeadship(Headship headship) {
        validateUpdateId(headship.getId());
        return sqlExecuteResult(updateById(headship));
    }

    /**
     * 查询一条职务信息
     * @param headshipId 职务信息ID
     */
    @Override
    public Headship getHeadshipById(Long headshipId) {
        LambdaQueryWrapper<Headship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Headship::getId, headshipId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询职务信息
     * @param headshipQueryDTO 职务信息参数
     */
    @Override
    public Page<Headship> getHeadshipPage(HeadshipDTO.QueryDTO headshipQueryDTO) {
        Page<Headship> headshipPage = new Page<>(
                headshipQueryDTO.getPage(), headshipQueryDTO.getSize());
        LambdaQueryWrapper<Headship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, headshipQueryDTO);

        BaseDeleted deleted = headshipQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(headshipPage, queryWrapper);
        } else {
            return page(headshipPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 职务信息 数量
     * @param headshipQueryDTO 职务信息参数
     */
    @Override
    public Long getHeadshipCount(HeadshipDTO.QueryDTO headshipQueryDTO) {
        LambdaQueryWrapper<Headship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, headshipQueryDTO);

        BaseDeleted deleted = headshipQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 职务信息
     * @param headshipQueryDTO 职务信息参数
     */
    @Override
    public List<Headship> getHeadshipList(HeadshipDTO.QueryDTO headshipQueryDTO) {
        LambdaQueryWrapper<Headship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, headshipQueryDTO);

        BaseDeleted deleted = headshipQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<Headship> queryWrapper, HeadshipDTO.QueryDTO headshipQueryDTO) {

        String headNo = headshipQueryDTO.getHeadNo();
        queryWrapper.eq(StringUtils.isNotBlank(headNo), Headship::getHeadNo, headNo);

        String headCode = headshipQueryDTO.getHeadCode();
        queryWrapper.eq(StringUtils.isNotBlank(headCode), Headship::getHeadCode, headCode);

        String headName = headshipQueryDTO.getHeadName();
        queryWrapper.eq(StringUtils.isNotBlank(headName), Headship::getHeadName, headName);

        HeadshipLeaderFlag leaderFlag = headshipQueryDTO.getLeaderFlag();
        queryWrapper.eq(ObjectUtils.isNotEmpty(leaderFlag), Headship::getLeaderFlag, leaderFlag);

        addQueryWrapper(queryWrapper, headshipQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<Headship, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                Headship::getOrderNumber,
                Headship::getTenantId,
                Headship::getApplicationId,
                Headship::getStatus,
                Headship::getDeleted,
                Headship::getDeleteUserId,
                Headship::getDeleteTime,
                Headship::getRemark,
                Headship::getCreateUserId,
                Headship::getCreateTime,
                Headship::getUpdateUserId,
                Headship::getUpdateTime,
                Headship::getExtend1,
                Headship::getExtend2,
                Headship::getExtend3);
    }

}
